package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectDemo8 {
    public static void main(String[] args) {
        try {
            Class cls = Class.forName("reflect.Person");
            Constructor cs = cls.getConstructor();
            Object obj = cs.newInstance();
            Field field = cls.getDeclaredField("name");
            Field field1 = cls.getDeclaredField("age");
            field.setAccessible(true);
            field.set(obj,"李浩");
            System.out.println(field.get(obj));
            field.setAccessible(false);

            field1.setAccessible(true);
            field1.set(obj,25);
            System.out.println(field1.get(obj));
            field1.setAccessible(false);
            System.out.println(obj);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
}
